package sql

import (
	"fmt"
	"database/sql"
	"encoding/base64"
	"encoding/json"
)



type List struct {
	Id        int
	Types     string
	Name      string
	Username  string
	Password  string
	Appid     string
	Secret    string
	Url       string
	Desc      string
	Base64Str string
}

// 查询数据
func Query(query string) (result []List) {

	// 结果集合
	result = make([]List, 0)

	rows, err := Select(query)
	if err != nil {
		fmt.Printf("Failed to query data: %v\n", err)
		return
	}
	for rows.Next() {
		var list List

		var url sql.NullString

		err = rows.Scan(&list.Id, &list.Types, &list.Name, &list.Username, &list.Password, &list.Appid, &list.Secret, &url, &list.Desc)
		if err != nil {
			fmt.Printf("Failed to read data: %v\n", err)
			continue
		}

		/* // 检查字段是否为NULL并进行处理
		if url.Valid {
			list.Url = url.String
		} */
		list.Url = url.String

		/**
		 * 你想要对一个结构体进行Base64加密,你首先需要将该结构体序列化为字节流,然后再对这个字节流进行Base64编码。
		 */
		listJsonData, _ := json.Marshal(list)
		// 将字节数组转换为Base64编码的字符串
		list.Base64Str = base64.StdEncoding.EncodeToString(listJsonData)
		result = append(result, list)

	}
	rows.Close()
	return result
}
